#include <QtGui>

#include "colorBar.h"
#include "MainWindow.h"

 MainWindow::MainWindow()
{
    QWidget *widget = new QWidget;
    setCentralWidget(widget);

    field = new Field;
    colorBar *colBar = new colorBar(0, 1);
    field->loadColorBar(colBar);
    connect(field, SIGNAL(colorBarChanged(float,float)), colBar, SLOT(reload(float,float)));

    QCheckBox *grid = new QCheckBox("Grid");
    grid->setCheckState(Qt::Checked);
    connect(grid, SIGNAL(toggled(bool)), field, SLOT(grid(bool)));

    QCheckBox *vertexNames = new QCheckBox("Show vertex names");
    connect(vertexNames, SIGNAL(toggled(bool)), field, SLOT(vertexNames(bool)));

    QCheckBox *triangleNames = new QCheckBox("Show triangle names");
    connect(triangleNames, SIGNAL(toggled(bool)), field, SLOT(triangleNames(bool)));

    QCheckBox *contours = new QCheckBox("Show contours");
    connect(contours, SIGNAL(toggled(bool)), field, SLOT(contours(bool)));
    if (field->mode()!=fileParser::EScalar1){
        contours->setEnabled(false);
    }
    connect(field, SIGNAL(enableContours(bool)), contours, SLOT(setEnabled(bool)));

    QGridLayout *gridLayout = new QGridLayout;
    QVBoxLayout *vBoxLayout = new QVBoxLayout;

    vBoxLayout->addWidget(grid);
    vBoxLayout->addWidget(vertexNames);
    vBoxLayout->addWidget(triangleNames);
    vBoxLayout->addWidget(contours);

    gridLayout->addLayout(vBoxLayout, 1, 0);
    gridLayout->addWidget(field, 1, 1, 2, 1);
    gridLayout->addWidget(colBar, 1,3, 2,3);
    gridLayout->setColumnStretch(1, 10);
    gridLayout->setColumnStretch(3, 1);
    widget->setLayout(gridLayout);

    createActions();
    createMenus();
    createToolBars();

    setWindowTitle(tr("Varan"));
    setMinimumSize(480, 320);
    setGeometry(200, 100, 600, 355);
}

void MainWindow::createActions(){

    openAct = new QAction(QIcon(":/images/fileopen.svg"),tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

    exportImageAct = new QAction(QIcon(":/images/fileexport.svg"), tr("&Export image as"), this);
    exportImageAct->setShortcut(tr("Ctrl+E"));
    exportImageAct->setStatusTip(tr("Export image as"));
    connect(exportImageAct, SIGNAL(triggered()), this, SLOT(exportImage()));

    defaultViewAct = new QAction(QIcon(":/images/cursor-arrow.png"), tr("Default view"), this);
    defaultViewAct->setStatusTip(tr("Default view"));
    connect(defaultViewAct, SIGNAL(triggered()), field, SLOT(defaultViewToggled()));

    zoomInAct = new QAction(QIcon(":/images/zoom-in.svg"), tr("Zoom in"), this);
    zoomInAct->setStatusTip(tr("Zoom in"));
    connect(zoomInAct, SIGNAL(triggered()), field, SLOT(zoomInToggled()));

    zoomOutAct = new QAction(QIcon(":/images/zoom-out.svg"), tr("Zoom out"), this);
    zoomOutAct->setStatusTip(tr("Zoom out"));
    connect(zoomOutAct, SIGNAL(triggered()), field, SLOT(zoomOutToggled()));

    exitAct = new QAction(QIcon(":/images/exit.png"), "Exit", this);
    exitAct->setShortcut(tr("Ctrl+X"));
    exitAct->setStatusTip(tr("Exit the application"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

    aboutAct = new QAction(tr("About"), this);
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

}
void MainWindow::createMenus() {
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(openAct);
    fileMenu->addAction(exportImageAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    viewMenu = menuBar()->addMenu(tr("View"));
    viewMenu->addAction(defaultViewAct);
    viewMenu->addAction(zoomInAct);
    viewMenu->addAction(zoomOutAct);
    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAct);
}

void MainWindow::createToolBars() {
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->setIconSize(QSize(32,32));
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(exportImageAct);

    viewToolBar = addToolBar(tr("View"));
    viewToolBar->setIconSize(QSize(32,32));
    viewToolBar->addAction(defaultViewAct);
    viewToolBar->addAction(zoomInAct);
    viewToolBar->addAction(zoomOutAct);

    quitToolBar = addToolBar(tr("Exit"));
    quitToolBar->setIconSize(QSize(32,32));
    quitToolBar->addAction(exitAct);
}



void MainWindow::open()
{
     QString fileName = QFileDialog::getOpenFileName(this,
        "Open File", ".", "Dat Files (*.dat);;All Files (*.*)");
     if (!fileName.isNull() ){
         field->loadFile(fileName);
     }
}

void MainWindow::about(){
    QMessageBox::about(this, "About Varan",
             "Visualisation of Discretized Vector Fields on Surfaces");
}

void MainWindow::exportImage(){
     QString fileName = QFileDialog::getSaveFileName(this,
       "Export file as...", ".", "PostScript (*.ps);;JPEG Image (*.jpg)");
     if (!fileName.isNull()){
         field->exportImage(fileName);
     }
}

